package com.example.admin.goodnews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.utils.SharedPreUtil;

/**
 * 手机登录页面
 * Created by admin on 2016/9/28.
 */

public class PhoneLoginActivity extends AppCompatActivity {
    private TextInputLayout textInputLayout_phone;// 手机号输入框
    private TextInputLayout textInputLayout_password;//  密码输入框
    private RelativeLayout tv_cancel_login;// 标题栏返回按钮
    private Button btn_login_phonelogin;// 登陆按钮
    private Button btn_registe_phonelogin;// 注册按钮

    private String phone;// 获取输入的手机号
    private String password;// 获取输入的密码
    private EditText edittext_phone;
    private EditText edittext_password;

    private String sh_phone;// 获取SharedPreferences中保存的手机号
    private String sh_password;// 获取SharedPreferences中保存的密码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phonelogin);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        tv_cancel_login = (RelativeLayout) findViewById(R.id.tv_cancel_login);// 标题栏返回按钮
        btn_login_phonelogin = (Button) findViewById(R.id.btn_login_phonelogin);// 登陆按钮
        btn_registe_phonelogin = (Button) findViewById(R.id.btn_registe_phonelogin);// 注册按钮
    }

    private void initData() {
        textInputLayout_phone = (TextInputLayout) findViewById(R.id.til_phone_phonelogin);
        edittext_phone = textInputLayout_phone.getEditText();
        textInputLayout_phone.setHint("请输入手机号");
        textInputLayout_password = (TextInputLayout) findViewById(R.id.til_password_phonelogin);
        edittext_password = textInputLayout_password.getEditText();
        textInputLayout_password.setHint("请输入密码");
        TextChange textchange = new TextChange();
        edittext_phone.addTextChangedListener(textchange);// 手机号文本监听
        edittext_password.addTextChangedListener(textchange);// 密码文本监听
    }

    private void initEvent() {
        // 标题栏返回按钮
        tv_cancel_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 登录按钮
        btn_login_phonelogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreUtil.initSharedPreference(getApplicationContext());
                sh_phone = SharedPreUtil.getInstance().getUser().getPhone();
                sh_password = SharedPreUtil.getInstance().getUser().getPassword();
                // 判断sh_phone不为空避免空指针异常
                if (sh_phone != null && sh_password != null) {
                    // 判断SharedPreferences中是否包含有输入的手机号和密码
                    if (phone.equals(sh_phone) && password.equals(sh_password)) {
                        Intent intent_success = new Intent(PhoneLoginActivity.this, MainActivity.class);
                        startActivity(intent_success);
                    } else {
                        if (!phone.equals(sh_phone)) {
                            Snackbar.make(v, "没有账号？点击注册~", Snackbar.LENGTH_LONG).setAction("注册", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent_toregiste = new Intent(PhoneLoginActivity.this, RegisteActivity.class);
                                    startActivity(intent_toregiste);
                                }
                            }).show();
                        }
                    }
                }
            }
        });

        // 注册按钮
        btn_registe_phonelogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_registe = new Intent(PhoneLoginActivity.this, RegisteActivity.class);
                startActivityForResult(intent_registe, 101);
            }
        });
    }

    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            phone = edittext_phone.getText().toString();
            password = edittext_password.getText().toString();
            if (phone.length() == 11 && password.length() > 6) {
                btn_login_phonelogin.setEnabled(true);
            } else {
                btn_login_phonelogin.setEnabled(false);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == 102) {
            Bundle bundle = data.getExtras();
            String phone = bundle.getString("phone");
            String password = bundle.getString("password");
            edittext_phone.setText(phone);
            edittext_password.setText(password);
        }
    }
}
